package com.softgroup.breath.easyecgproject.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.softgroup.breath.easyecgproject.R;
import com.softgroup.breath.easyecgproject.entry.UserInfo;
import com.softgroup.breath.easyecgproject.views.CheckBoxSex;
import com.softgroup.breath.easyecgproject.views.NumberRulerView;
import com.softgroup.breath.easyecgproject.windowbase.ToastUtil;

public class DialogUtil implements View.OnClickListener {
    private final String TAG = DialogUtil.class.getSimpleName();
    private Dialog dialog;
    private static DialogUtil dialogUtil;
    private View viewUserSetting;
    private View viewCarInfo;
    private View viewConfirm;
    private TextView tv_confirmInfo;
    private int mode;
    public static final int MODE_STANDBY = 0;
    public static final int MODE_START = 1;
    private EditText et_name;
    private NumberRulerView nrv_age;
    private CheckBoxSex cbs_sex;
    private TextView tv_confirmUser;
    private UserInfo userInfo;
    private Context context;
    private View viewTestSetting;
    private TextView tv_confirmTest;
    private NumberRulerView numberRulerView;
    private Button bt_take;


    private DialogUtil(Context context) {
        dialog = new Dialog(context);
        userInfo = new UserInfo();
        this.context = context;
        initView(context);
    }

    public static DialogUtil getInstance(Context context) {
        if (dialogUtil == null) {
            synchronized (DialogUtil.class) {
                dialogUtil = new DialogUtil(context);
            }
        }
        return dialogUtil;
    }

    private void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        viewConfirm = layoutInflater.inflate(R.layout.dialog_confirm, null);
        tv_confirmInfo = viewConfirm.findViewById(R.id.tv_confirm_info);
        TextView tv_confirmYes = viewConfirm.findViewById(R.id.tv_confirm_yes);
        tv_confirmYes.setOnClickListener(this);
        TextView tv_confirmNo = viewConfirm.findViewById(R.id.tv_confirm_no);
        tv_confirmNo.setOnClickListener(this);
        viewCarInfo = layoutInflater.inflate(R.layout.dialog_car_info, null);
        viewUserSetting = layoutInflater.inflate(R.layout.dialog_user_setting, null);
        et_name = viewUserSetting.findViewById(R.id.et_name);
        nrv_age = viewUserSetting.findViewById(R.id.nrv_age);
        cbs_sex = viewUserSetting.findViewById(R.id.cbs_user);
        bt_take = viewUserSetting.findViewById(R.id.bt_take);
        bt_take.setOnClickListener(this);
        tv_confirmUser = viewUserSetting.findViewById(R.id.tv_confirm);
        tv_confirmUser.setOnClickListener(this);
        viewTestSetting = layoutInflater.inflate(R.layout.dialog_test_setting, null);
        numberRulerView = viewTestSetting.findViewById(R.id.nrv_test_duration);
        numberRulerView.setMax(100);
        numberRulerView.setMin(0);
        numberRulerView.setUnit("分钟");
        numberRulerView.setText("请选择时长！");
        tv_confirmTest = viewTestSetting.findViewById(R.id.tv_confirm_test);
        tv_confirmTest.setOnClickListener(this);
    }

    public void showUserSetting() {
        dialog.setContentView(viewUserSetting);
        et_name.setText("");
        nrv_age.setValue(0);
        nrv_age.setText("请选择年龄：");
        nrv_age.setUnit("岁");
        cbs_sex.setCheck(true);
        dialog.show();
    }


    public void showConfirm(int mode) {
        this.mode = mode;
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            switch (mode) {
                case MODE_STANDBY:
                    dialog.setContentView(viewConfirm);
                    tv_confirmInfo.setText(R.string.standby_mode_confirm);
                    dialog.show();
                    break;
                case MODE_START:
                    dialog.setContentView(viewConfirm);
                    tv_confirmInfo.setText(R.string.start_confirm);
                    dialog.show();
                    break;
                case 2:
                    break;
            }
            dialog.show();
        }
    }

    public void showCarInfo() {
        dialog.setContentView(viewCarInfo);
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            dialog.setContentView(viewCarInfo);
            dialog.show();
        }
    }


    private OnConfirmListener onConfirmListener;

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm_yes:
                if (onConfirmListener != null) {
                    onConfirmListener.onConfirm(mode);
                }
                dialog.dismiss();
                break;
            case R.id.tv_confirm_no:
                if (onConfirmListener != null) {
                    onConfirmListener.onRefuse(mode);
                }
                dialog.dismiss();
                break;
            case R.id.tv_confirm:
                if (et_name.getText() != null) {
                    userInfo.setUsername(et_name.getText().toString());
                    userInfo.setUsersex(cbs_sex.isCheck() ? "男" : "女");
                    userInfo.setUserage(nrv_age.getValue() + "");
                    dialog.dismiss();
                    if (onConfirmListener != null) {
                        onConfirmListener.onUser(userInfo);
                    }
                } else {
                    ToastUtil.getInstance().showToast(0);
                }
                break;
            case R.id.tv_confirm_test:
                SettingManager.getInstance().setTestDuration(numberRulerView.getValue());
                dialog.dismiss();
                break;
            case R.id.bt_take:
                if (onConfirmListener != null) {
                    onConfirmListener.onTake();
                }
                break;

        }

    }

    public void showTestSetting() {
        dialog.setContentView(viewTestSetting);
        numberRulerView.setValue(SettingManager.getInstance().getTestDuration());
        dialog.show();
    }

    public interface OnConfirmListener {
        void onConfirm(int mode);

        void onRefuse(int mode);

        void onUser(UserInfo userInfo);

        void onTake();
    }
}
